-- UICommentDesk
-- Created by cheny3 Aug/29/2016
-- Peter的木桌

UICommentDesk = class("UICommentDesk", function()
    return cc.Layer:create();
end);

function UICommentDesk.create(gridData)
    return UICommentDesk.new(gridData);
end

-- 构造函数
function UICommentDesk:ctor(gridData)
    self:setName("UICommentDesk");

    -- 复用座钟界面
    local node = cc.CSLoader:createNode("layout/dungeon/special/DeskClock.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.gridData  = gridData;

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UICommentDesk:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UICommentDesk:registerEventCallback()
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UICommentDesk 界面析构清理");
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UICommentDesk:registerTouchEvent()
    -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UICommentDesk");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    -- 按钮回调
    local btnNode = findChildByName(node, "CT/btn_node");
    local btn1 = btnNode:getChildByName("btn_hour");
    local btn2 = btnNode:getChildByName("btn_minute");
    local btn3 = btnNode:getChildByName("btn_open");

    -- 《来自开发组的一封信》
    -- 注册按钮点击事件
    local function onbtnClick1(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");

            showGameBoard(-1);
        end
    end
    AddTouchEventListener(btn1, onbtnClick1);
    btn1:setTitleText(getLocStr("peter_desk_1"));
    TextStyleM.setTextStyle(btn1, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);

    -- 《Peter的一封信》
    -- 注册按钮点击事件
    local function onbtnClick2(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");

            showGameBoard(-3);
        end
    end
    AddTouchEventListener(btn2, onbtnClick2);
    btn2:setTitleText(getLocStr("peter_desk_2"));
    TextStyleM.setTextStyle(btn2, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);

    -- 评论界面
    -- 注册按钮点击事件
    local function onbtnClick3(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");

            local pos = self.gridData:getPos();
            local grid = DungeonM.getGridByPos(pos);
            if grid and grid.limitUrl then
                -- 创建格子的时候发现地址是不通的
                alert(getLocStr("comment_url_fail"));
                UIMgr.getCurrentScene():removeFormByName("UICommentDesk");
                return;
            end

            local url = MaintainBulletinM.getPackageCommentUrl();
            if url == "" then
                alert(getLocStr("comment_url_fail"));
                UIMgr.getCurrentScene():removeFormByName("UICommentDesk");
                return;
            end

            -- 部分情况屏蔽
            if not canShowPeterDesk() then
                alert(getLocStr("comment_url_fail"));
                UIMgr.getCurrentScene():removeFormByName("UICommentDesk");
                return;
            end

            -- 打开链接
            cc.Application:getInstance():openURL(url);

            local function okFunc()
                DungeonActionM.go("take_comment_bonus", pos);
            end

            local grid = DungeonM.getGridByPos(self.gridData:getPos());
            local bonusList = BuildingBonusM.calcNormalObjectBonus(ME.user, grid);

            -- 弹框
            local function delay()
                UIMgr.getCurrentScene():removeFormByName("UICommentDesk");
                confirm2(CONFIRM_TYPE_OK, getLocStr("strange_sounds"), buildingInfo["dialog2"],
                    okFunc, nil, nil, nil, {["hideBackBtn"] = true});
            end

            if BuffDataM.query("has_comment", 1) == 0 then
                -- 没领过奖励，给奖励
                performWithDelay(self.node, delay, 2);
                wait(2);
            end
        end
    end
    AddTouchEventListener(btn3, onbtnClick3);
    btn3:setTitleText(buildingInfo["view_text"]);
    TextStyleM.setTextStyle(btn3, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
end

-- 重绘
function UICommentDesk:redraw()
    -- 查询建筑配置
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    local descLabel  = findChildByName(self.node, "CT/desc");
    local iconImg    = findChildByName(self.node, "CT/icon");
    local titleLabel = findChildByName(self.node, "CT/title_node/title");

    titleLabel:setString(buildingInfo["title"]);
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    iconImg:loadTexture(iconPath);
    local iconSize = iconImg:getContentSize();

    iconImg:setScale(1.3);
    iconImg:setPositionY(iconImg:getPositionY() - 20);
    TextStyleM.setArtWordTitleStyle(titleLabel);
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);

    self.dialog = buildingInfo["dialog"];

    applyString(descLabel, self.dialog);
end